package com.wj.console.generator.controller;

import java.awt.Desktop;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import sun.nio.cs.Surrogate;

@Controller
@RequestMapping("generator")
public class GeneratorController {

	public void generator(HttpServletRequest request,String tableName)throws Exception{
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(
		        Surrogate.Generator.class.getResourceAsStream("/resources/generatorConfig.xml"));
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}
	public static void main(String[] args)throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(
		        Surrogate.Generator.class.getResourceAsStream("/resources/generatorConfig.xml"));
		config.getContexts().get(0).getTableConfigurations().get(0).setTableName("idcard_auth_info");
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
		//打开生成目录
		Desktop.getDesktop().open(new File(config.getContexts().get(0).getJavaModelGeneratorConfiguration().getTargetProject()));
		System.exit(0);
	}
}
